﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myLittleC
{

    class SimpleCharsTable
    {
        SortedList<String, SimpleCharType> Defenitions = new SortedList<string, SimpleCharType>();

        private IDictionary<String, SimpleCharType> Chars { get { return Defenitions; }}
        private int NameSize { get; set; }
        public SimpleCharsTable() { NameSize = 4; }
        public int find(String Name) { return Defenitions.IndexOfKey(Name); }
        public bool isDefenition(String Name) { return find(Name) != -1; }


        public void Add(String Name, SimpleCharType Type) 
        {
            if (isDefenition(Name))  throw new ComExeption(ERRORS.DublDeclaration);  
            
            Chars.Add(Name, Type);
            NameSize = (NameSize < Name.Length) ? Name.Length : NameSize;
        }

        public override String ToString()
        {
            String res = String.Format(" ─{0}─ ──────────── \n", repString("─", NameSize)) +
                         String.Format("│ {0}Name │    Type    │\n", repString(" ", NameSize - 4 )) +
                         String.Format(" ─{0}─ ──────────── \n", repString("─", NameSize));
                
            foreach (KeyValuePair<string, SimpleCharType> item in Chars)
            {
                res +=
                String.Format("│ {0}{1} │    {2}{3}│\n", 
                    repString(" ", NameSize - 1), item.Key.ToString(), item.Value.ToString(), repString(" ", item.Value.ToString().Length -5) ) +
                String.Format(" ─{0}─ ──────────── \n", repString("─", NameSize ));
                //res +="|" + item.Key.ToString() + "  |  " + item.Value.ToString();
            }
            
            return res;
        }

        private String repString(String c, int leng) { return leng == 0 ? c : c + repString(c, leng - 1); }
    }
}
